<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\DeviceType;

class DevicetypeController extends Controller
{
    
    protected $message = [
        
        'dt_typeid.required' => '设备类型编号必须',
        'dt_typeid.integer' => '设备类型编号必须是数字',
        
        'dt_typename.required' => '设备类型名称必须',
        'dt_typename.max' => '设备类型名称不能超过64个字符',
        
        'dt_rtdata_table.required' => '实时数据表名必须',
        'dt_rtdata_table.max' => '实时数据表名不能超过64个字符',
        
        'dt_hisdata_table.required' => '历史数据表名必须',
        'dt_hisdata_table.max' => '历史数据表名不能超过64个字符',

    ];
    

    /**
     * @api {get} /devicetype/ 设备分类
     * @apiDescription 设备分类列表
     * @apiGroup 设备分类管理
     * @apiPermission none
     * @apiParam {String} [dt_typename] 分类名称
     * @apiParam {Number} [pageSize=15] 每页显示条数 
     * @apiSuccess {String} code 状态标志 
     * @apiSuccess {String} message 提示信息 
     * @apiSuccess {Object[]} data  区域数据
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         "code": 0,
     *         "message": "",
     *          "data": [
     *             {
     *             }
     *          ],
     *          areaName: {}
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "错误提示"
     *     }
     */
    public function index(Request $request)
    {
        $perPage = $request->input('pageSize',15);
        $name = $request->input('dt_typename', '');
        $query = DeviceType::query();
        if(!empty($name)) {
            $query->where('dt_typename', 'like', $name.'%');
        }
        $users = $query->with('children')-> paginate($perPage);
        
        return $users;
    }

    /**
     * @api {post} /devicetype/ 添加
     * @apiDescription 添加设备分类
     * @apiGroup 设备分类管理
     * @apiPermission none
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         "code": 0,
     *         "message": "",
     *              
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "添加失败"
     *     }
     */
    public function store(Request $request)
    {
       $data = $request->input();
       $this->validateData($request);
       $ret = DeviceType::create($data);
       
       if(!$ret){
           $this->error('添加失败');
       }
       $this->addAction(['t_devicetype']);
    }


    public function show($id)
    {
        
    }


    /**
     * @api {put} /devicetype/:id 更新
     * @apiDescription 更新设备分类
     * @apiGroup 设备分类管理
     * @apiPermission none
     * @apiParam {Number} id 设备分类编号
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         "code": 0,
     *         "message": "",
     *              
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "保存失败"
     *     }
     */
    public function update(Request $request, $id)
    {
       $deviceType = DeviceType::findOrFail($id);
       $data = $request->input();
       $this->validateData($request);
       $ret = $deviceType->update($data);
       
       if(!$ret){
           $this->error('保存失败');
       }
       $this->addAction(['t_devicetype']);
    }
    
    //验证表单
    protected function validateData($request) {
        $this->validate($request, [
            'dt_typeid' => 'required|integer',
            'dt_typename' => 'required|max:64',
            'dt_rtdata_table' => 'required|max:64',
            'dt_hisdata_table' => 'required|max:64'
        ], $this->message);
    }

    /**
     * @api {delete} /devicetype/:id 删除
     * @apiDescription 删除设备分类
     * @apiGroup 设备分类管理
     * @apiPermission none
     * @apiParam {Number} id 设备分类编号
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         
     *          "code": 0,
     *          "message": "",
     *        
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "错误提示"
     *     }
     */
    public function destroy($id)
    {
       $deviceType = DeviceType::findOrFail($id);
       if(!$deviceType->delete()){
           $this->error('删除失败');
       }
       $this->addAction(['t_devicetype']);
    }
}
